home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / idents.c < prev    next >
C/C++ Source or Header  |  1993-03-22  |  8KB  |  231 lines

  1. /* $Id: Idents.c,v 1.12 1992/05/05 13:19:05 grosch rel $ */
  2.  
  3. /* $Log: Idents.c,v $
  4.  * Revision 1.12  1992/05/05  13:19:05  grosch
  5.  * added rcsid
  6.  *
  7.  * Revision 1.11  1992/02/18  12:52:30  grosch
  8.  * changed tString from unsigned char * to char *
  9.  *
  10.  * Revision 1.10  1992/01/31  16:31:44  grosch
  11.  * adaption to ANSI C
  12.  *
  13.  * Revision 1.9  1992/01/30  13:13:54  grosch
  14.  * redesign of StringMem: pointer instead of array index
  15.  *
  16.  * Revision 1.8  1992/01/14  15:24:35  grosch
  17.  * introduced automatic initialization
  18.  *
  19.  * Revision 1.7  1991/11/21  14:28:16  grosch
  20.  * new version of RCS on SPARC
  21.  *
  22.  * Revision 1.6  91/09/18  15:18:46  grosch
  23.  * added procedure GetStringRef
  24.  * 
  25.  * Revision 1.5  91/07/17  17:23:07  grosch
  26.  * introduced ARGS trick for ANSI compatibility
  27.  * 
  28.  * Revision 1.4  91/01/21  12:13:20  grosch
  29.  * some performance improvements
  30.  * 
  31.  * Revision 1.3  90/09/20  09:12:22  grosch
  32.  * calmed down lint
  33.  * 
  34.  * Revision 1.2  90/07/04  14:33:55  grosch
  35.  * introduced conditional include
  36.  * 
  37.  * Revision 1.1  89/06/06  10:28:25  grosch
  38.  * added public variable NoIdent
  39.  * 
  40.  * Revision 1.0  88/10/04  11:44:38  grosch
  41.  * Initial revision
  42.  * 
  43.  */
  44.  
  45. /* Ich, Doktor Josef Grosch, Informatiker, Sept. 1987 */
  46.  
  47. static char rcsid [] = "$Id: Idents.c,v 1.12 1992/05/05 13:19:05 grosch rel $";
  48.  
  49. # include "ratc.h"
  50. # include "Idents.h"
  51. # include "DynArray.h"
  52.  
  53. # define InitialTableSize    1024
  54. # define HashTableSize        256
  55. # define cNoIdent        0
  56.  
  57. tIdent    NoIdent = 1;
  58.  
  59. typedef struct {
  60.    tStringRef    String;
  61.    cardinal    Length;
  62.    tIdent    Collision;
  63. } IdentTableEntry;
  64.  
  65. static    unsigned short    Null        = 0;
  66. static    IdentTableEntry    Table [InitialTableSize] = {{0, 0, 0}, {& Null, 0, cNoIdent}};
  67. static    IdentTableEntry    * TablePtr    = Table;
  68. static    unsigned long    IdentTableSize    = InitialTableSize;
  69. static    tIdent        IdentCount    = 1;
  70.  
  71. static    tIdent        HashTable [HashTableSize] = {
  72.    1       , cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  73.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  74.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  75.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  76.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  77.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  78.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  79.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  80.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  81.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  82.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  83.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  84.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  85.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  86.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  87.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  88.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  89.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  90.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  91.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  92.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  93.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  94.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  95.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  96.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  97.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  98.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  99.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  100.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  101.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  102.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  103.    cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent, cNoIdent,
  104. };
  105.  
  106. tIdent MakeIdent
  107. # ifdef __STDC__
  108.    (register char * string, register cardinal length)
  109. # else
  110.    (string, length)
  111.    register char *    string;
  112.    register cardinal    length;
  113. # endif
  114.    {
  115.       register    cardinal    HashTableIndex;
  116.       register    tIdent        CurIdent;
  117.       register    IdentTableEntry    * TablePtrReg;
  118.    
  119.       HashTableIndex = length == 0 ? 0 :    /* hash */
  120.      ((cardinal) * string + (cardinal) string [length - 1] * 11
  121.                   + length * 26) & (HashTableSize - 1);
  122.  
  123.       CurIdent = HashTable [HashTableIndex];    /* search */
  124.       while (CurIdent != cNoIdent) {
  125.      TablePtrReg = & TablePtr [CurIdent];
  126.      if (TablePtrReg->Length == length && IsEqualSt (TablePtrReg->String, string))
  127.         return CurIdent;            /* found */
  128.      CurIdent = TablePtrReg->Collision;
  129.       }
  130.  
  131.       if (++ IdentCount == IdentTableSize)    /* not found: enter */
  132.      ExtendArray ((char * *) & TablePtr, & IdentTableSize, (long) sizeof (IdentTableEntry));
  133.       TablePtrReg = & TablePtr [IdentCount];
  134.       TablePtrReg->String    = PutString (string, length);
  135.       TablePtrReg->Length    = length;
  136.       TablePtrReg->Collision    = HashTable [HashTableIndex];
  137.       HashTable [HashTableIndex] = IdentCount;
  138.       return IdentCount;
  139.    }
  140.  
  141. void GetString
  142. # ifdef __STDC__
  143.    (tIdent ident, char * string)
  144. # else
  145.    (ident, string)
  146.    tIdent    ident;
  147.    char *    string;
  148. # endif
  149.    {
  150.       StGetString (TablePtr [ident].String, string);
  151.    }
  152.  
  153. tStringRef GetStringRef
  154. # ifdef __STDC__
  155.    (tIdent ident)
  156. # else
  157.    (ident) tIdent ident;
  158. # endif
  159.    {
  160.       return TablePtr [ident].String;
  161.    }
  162.  
  163. tIdent MaxIdent ()
  164.    {
  165.       return IdentCount;
  166.    }
  167.  
  168. void WriteIdent
  169. # ifdef __STDC__
  170.    (FILE * file, tIdent ident)
  171. # else
  172.    (file, ident)
  173.    FILE *    file;
  174.    tIdent    ident;
  175. # endif
  176.    {
  177.       char    string [256];
  178.  
  179.       GetString (ident, string);
  180.       (void) fputs (string, file);
  181.    }
  182.  
  183. void WriteIdents ()
  184.    {
  185.       cardinal    i;
  186.  
  187.       for (i = 1; i <= IdentCount; i ++) {
  188.      (void) printf ("%5d ", i);
  189.      WriteIdent (stdout, i);
  190.      (void) fputc ('\n', stdout);
  191.       }
  192.    }
  193.  
  194. void WriteHashTable ()
  195.    {
  196.       tIdent    CurIdent;
  197.       cardinal    i;
  198.       cardinal    Count;
  199.    
  200.       for (i = 0; i < HashTableSize; i ++) {
  201.      (void) printf ("%5d", i);
  202.  
  203.      Count = 0;
  204.      CurIdent = HashTable [i];
  205.      while (CurIdent != cNoIdent) {
  206.         Count ++;
  207.         CurIdent = TablePtr [CurIdent].Collision;
  208.      }
  209.      (void) printf ("%5d", Count);
  210.  
  211.      CurIdent = HashTable [i];
  212.      while (CurIdent != cNoIdent) {
  213.         (void) fputc (' ', stdout);
  214.         WriteIdent (stdout, CurIdent);
  215.         CurIdent = TablePtr [CurIdent].Collision;
  216.      }
  217.      (void) fputc ('\n', stdout);
  218.       }
  219.       (void) printf ("\nIdents = %5d\n", IdentCount);
  220.    }
  221.     
  222. void InitIdents ()
  223.    {
  224.       register cardinal i;
  225.  
  226.       for (i = 0; i < HashTableSize; i ++) HashTable [i] = cNoIdent;
  227.  
  228.       IdentCount    = 0;
  229.       NoIdent        = MakeIdent ("", 0);
  230.    }
  231.